﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using FBEntities.FBObjects;
using XDAFacebook;
using XFControls.Utils;
using XFControls.XFPanels;
using XFControls.XFPanels.XFPanelItems;
using XFSenseUI.XFItems;

namespace XFItemControls.XFPanels.XFPanelItems
{
    public class XFItemAlbum : XFItemBase
    {
        private readonly FBAlbum _album;
        private readonly int _photoHeight;

        private Rectangle _albumPicture;
        private Rectangle _albumImageCount;
        private Rectangle _albumDescription;

        public XFItemAlbum(FBAlbum album)
        {
            _photoHeight = GetWidth() >> 2;
            _album = album;

            Style = new XFItemStyle
                        {
                            BackgroundColor = Color.White,
                            TextFont = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Bold),
                            TextColor = Color.Black,
                            SecondaryTextFont = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular),
                            SecondaryTextColor = Color.DarkGray,
                            DashStyleBottom = DashStyle.Solid,
                            BorderBottomColor = Color.DarkGray,
                            Padding = 5,
                            SelectedBackgroundColor = Color.Lavender,
                            SelectedTextColor = Color.Black,
                            SelectedSecondaryTextColor = Color.DarkGray
                        };

            ItemType = XFItemType.Clickable;
        }

        public override int GetHeight()
        {
            if (!Visible) return 0;

            int width = GetWidth();
            int currX = Style.PaddingLeft + Style.PaddingRight + _photoHeight;
            int currY = Style.PaddingTop;

            width -= currX + Style.PaddingRight;

            _albumPicture = new Rectangle(Style.PaddingLeft, Style.PaddingTop, _photoHeight, _photoHeight);
            _albumDescription = new Rectangle(currX, currY, width, 0);

            var mes = Style.TextFont.MeasureString(_album.name, _albumDescription);

            currY += mes.Height + (Style.PaddingTop << 1);

            _albumDescription.Height = mes.Height;
            _albumImageCount = new Rectangle(currX, currY, width, 0);


            return _photoHeight + Style.PaddingTop + Style.PaddingBottom;
        }

        public override void ItemPaint(Graphics g, int x, int y)
        {
            base.ItemPaint(g, x, y);

            Color textColor = getCurrentFontColor(false);
            Color secondaryTextColor = getCurrentFontColor(true);

            DrawImage(_album.picture, g, _albumPicture);
            g.DrawString(_album.name, _albumDescription, Style, textColor);

            g.DrawString(_album.count + " " + Settings.LocalizationStrings.Photos,
                         _albumImageCount,
                         Style,
                         Style.SecondaryTextFont,
                         secondaryTextColor);
        }

        public override XFItemClickResult GetClickResult()
        {
            var panel = new XFPanelList();
            var back = new XFItemBackHeader();
            panel.ShowScrollbar(true);
            panel.ScrollbarColor = Parent.ScrollbarColor;
            panel.Add(back);

            var worker = new XFWorkerLoadAlbums(_album);
            var loading = new XFItemLoading(worker);

            loading.WorkFinished += (o, e) =>
                                        {
                                            if (panel.IsDisposed) return;

                                            panel.Invoke(new Action(() =>
                                                                        {
                                                                            panel.ClearItemsAfterWaiting(loading);
                                                                            var list = new List<FBPhoto>();

                                                                            foreach (FBPhoto photo in _album.Photos.data)
                                                                            {
                                                                                list.Add(photo);
                                                                                if (list.Count != Settings.PhotosPerLine)
                                                                                    continue;
                                                                                panel.Add(
                                                                                    new XFItemPictureRow(list));
                                                                                list = new List<FBPhoto>();
                                                                            }
                                                                            if (list.Count > 0)
                                                                                panel.Add(new XFItemPictureRow(list));

                                                                            var next = new SenseItem
                                                                                           {
                                                                                               Tag = _album,
                                                                                               MainText = Settings.LocalizationStrings.LoadMore
                                                                                           };

                                                                            next.Click += next_Click;
                                                                            if (_album.Photos.paging != null)
                                                                                panel.Add(next);
                                                                        }));
                                        };

            panel.Add(loading);
            Parent.SetRightPanel(panel);
            return XFItemClickResult.SlideLeft;
        }

        private void next_Click(object sender, EventArgs e)
        {
            var button = sender as SenseItem;
            var parent = button.Parent as XFPanelList;

            parent.ClearItem(parent[button]);

            var oldAlbum = button.Tag as FBAlbum;
            var worker = new XFWorkerLoadAlbums(oldAlbum, oldAlbum.Photos.paging.next);
            var loading = new XFItemLoading(worker);

            loading.WorkFinished += (o, ev) =>
                                        {
                                            FBAlbum album = null;
                                            if (ev.Worker is XFWorkerLoadAlbums)
                                                album = ((XFWorkerLoadAlbums)ev.Worker).Album;

                                            var par = ((XFItemLoading)o).Parent as XFPanelList;

                                            if (!par.IsDisposed)
                                                par.Invoke(new Action(() =>
                                                                          {
                                                                              par.ClearItemsAfterWaiting(loading);

                                                                              var list = new List<FBPhoto>();

                                                                              foreach (
                                                                                  FBPhoto photo in _album.Photos.data)
                                                                              {
                                                                                  list.Add(photo);
                                                                                  if (list.Count ==
                                                                                      Settings.PhotosPerLine)
                                                                                  {
                                                                                      par.Add(new XFItemPictureRow(list));
                                                                                      list = new List<FBPhoto>();
                                                                                  }
                                                                              }
                                                                              if (list.Count > 0)
                                                                                  par.Add(new XFItemPictureRow(list));

                                                                              var next = new SenseItem
                                                                                             {
                                                                                                 Tag = _album,
                                                                                                 MainText = Settings.LocalizationStrings.LoadMore
                                                                                             };

                                                                              next.Click += next_Click;
                                                                              if (_album.Photos.paging != null &&
                                                                                  !string.IsNullOrEmpty(
                                                                                      _album.Photos.paging.next))
                                                                                  par.Add(next);
                                                                          }));
                                        };

            button.Dispose();
            parent.Add(loading);
        }
    }
}